package cn.entity.master;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class master1 extends JPanel implements masterImp {

    private int index;
    private int x;
    private int y;
    public  int width;
    private int a;
    private int step;
    private int hitSpeed;
    private int hitSpeed1;
    private int bigTime;
    private BufferedImage[] images;
    private BufferedImage image;
    private long moveStartTime = -1; // 记录移动开始时间
    private int originalX;          // 记录初始X位置
    private int originalY;          // 记录初始Y位置
    private boolean returning = false; // 是否正在返回状态
    private Timer moveTimer;
    private int speed = 20;

public master1(int x,int y,int width) throws IOException {
    hitSpeed = 0;
    this.width=width;
    step=0;
    bigTime=0;
    a=0;
    this.x=x;
    this.y=y;
    this.originalX = x; // 保存初始位置
    this.originalY = y;

        images = new BufferedImage[4];
        for(int i = 0; i < images.length; i++) {
            images[i]= ImageIO.read(new File("img/master/enemy0."+i+".png"));
        }
        image=images[0];

    }
    public void paint(Graphics g){
        g.drawImage(getImage(),getX(),getY(),null);
        g.setColor(Color.RED);
        g.fillRect(getX(),getY()-40,width,5);

    }
    public void setHp(int hit,int level) {
        if (hit == 1) {
            if (width > 0) {
                if (width > 30) {
                    hitSpeed++;
                    if (hitSpeed >= 3) {
                        width -= 1 + (level - 1);
                        hitSpeed = 0;
                    }

                } else {
                    hitSpeed++;
                    if (hitSpeed == 6) {
                        width -= 1 + (level / 2);
                        hitSpeed = 0;
                    }
                }
            }
        }
        if (hit == 2) {
            if (width > 0) {
                if (width > 30) {
                    width -= 3 + (level - 1);
                } else width -= 1;
            }
        }
        if (hit == 3) {
            if (width > 0) {
                if (width > 30) {
                    hitSpeed1++;
                    if (hitSpeed1 >= 3) {
                        width -= 2;
                        hitSpeed1 = 0;
                    }
                } else width -= 1;
            }

        }
    }

    public void startMovingToPlayer(int playerX, int playerY) {
        if (moveStartTime != -1) return;
        moveStartTime = System.currentTimeMillis();
        double dx = playerX - this.x;
        double dy = playerY - this.y;
        double length = Math.sqrt(dx * dx + dy * dy);

        if (length > 0) {
            double directionX = dx / length;
            double directionY = dy / length;
            if (moveTimer != null && moveTimer.isRunning()) {
                moveTimer.stop();
            }

            moveTimer = new Timer(50, e -> {
                long elapsed = System.currentTimeMillis() - moveStartTime;

                if (elapsed < 3500) {
                    int newX = (int)(x + directionX * speed);
                    int newY = (int)(y + directionY * speed);
                    if (newX > 0 && newX < 752 && newY > 0 && newY < 560) {
                        x = newX;
                        y = newY;
                    }
                } else {
                    x = originalX;
                    y = originalY;
                    moveTimer.stop();
                    moveStartTime = -1;
                }
            });
            moveTimer.start();
        }
    }


    public boolean isMoving() {
        return moveStartTime != -1;
    }
    @Override
    public int getX() {
        return x;
    }
    @Override
    public int getY() {
        return y;
    }
    public int getIndex(){
        return index;
    }
    public void setIndex(int index){
        this.index = index;
    }
    public int getImageLength(){
        return images.length;
    }
    public void setImage(int index){
        this.image = images[index];
    }
    public BufferedImage getImage(){
        return image;
    }
    @Override
    public int getWidth() {
        return image.getWidth();
    }
    public int getHitWidth(){
        return width;
    }
    @Override
    public int getHeight() {
        return image.getHeight();
    }
    public void setY(int y){
        this.y=y;
    }
}




